package com.ym.b2b.salesman.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.salesman.service.EmBSalesmanService;
import com.ym.b2b.salesman.sqlbean.EmBSalesman;
import com.ym.b2b.user.service.EmCUserService;
import com.ym.b2b.user.sqlbean.EmCUser;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;
import com.zc.export.ExcelUtil;

/**
 * @ClassName: EmBSalesmanController
 * @Author tangxj
 * @Date 2015-3-18 上午11:21:47
 * @Modif
 * @CopyRight B2B小组
 */
@Controller
@RequestMapping("/salesman")
public class EmBSalesmanController extends BaseController {

    @Autowired
    private EmBSalesmanService emBSalesmanService;
    @Autowired
    private EmCUserService     emCUserService;

    @RequestMapping("searchPageSalesman.do")
    @ResponseBody
    public Result searchPageSalesman(EmBSalesman emBSalesman, Page<EmBSalesman> page, HttpServletRequest req) {

        Result result = new Result();
        try {
            result = emBSalesmanService.searchPageEmBSalesman(page, emBSalesman);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;

    }

    @RequestMapping(value = "searchPageSalesmanExcel.do")
    @ResponseBody
    public Result searchPageSalesmanExcel(EmBSalesman emBSalesman, Page<EmBSalesman> page, String loadType) {
        Result ret = new Result();
        try {
            if (loadType == null || loadType.equals("")) {
                ret.setCode(Result.CODE_100);
                ret.setResult("loadType 参数 '数量'  不能为空!");
                return ret;
            }
            page.setPageable("true");
            page.setPageSize(Integer.parseInt(loadType));
            page.setStartRow(1);
            ret = emBSalesmanService.searchPageEmBSalesman(page, emBSalesman);
            ExcelUtil<EmBSalesman> excelUtil = new ExcelUtil<EmBSalesman>();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("model", ret.getRows());
            ret = excelUtil.createExcel("salesmanListExcel", map);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;
    }

    @RequestMapping("showEmBSalesmanDetail.do")
    @ResponseBody
    public Result showEmBSalesmanDetail(EmBSalesman emBSalesman, HttpServletRequest req) {
        Result result = new Result();
        try {
            result = emBSalesmanService.showEmBSalesmanDetail(emBSalesman);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "insertSalesman.do")
    @ResponseBody
    public Result insertSalesman(EmBSalesman emBSalesman, HttpServletRequest req) {
        Result result = new Result();
        try {
            result = emBSalesmanService.insertEmBSalesman(emBSalesman);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    @RequestMapping("updateSalesman.do")
    @ResponseBody
    public Result updateSalesman(EmBSalesman emBSalesman, HttpServletRequest req) {
        Result result = new Result();
        try {
        	if("1".equals(String.valueOf(emBSalesman.getSalesmanType()))){
        		emBSalesman.setAddBuyer(new BigDecimal(0));
        		emBSalesman.setIsAgentProvider(new BigDecimal(0));
        	}
            result = emBSalesmanService.updateEmBSalesman(emBSalesman);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    // 删除一条记录
    @RequestMapping("deleteSalesman.do")
    @ResponseBody
    public Result deleteSalesman(EmBSalesman emBSalesman, HttpServletRequest req) {
        Result result = new Result();
        try {
            result = emBSalesmanService.deleteEmBSalesman(emBSalesman);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    // 删除多条记录内容
    @RequestMapping("deleteMultipleEmBSalesmans.do")
    @ResponseBody
    public Result deleteMultipleEmBSalesmans(String salesmanIdList, HttpServletRequest req) {
        Result result = new Result();
        try {
            result = emBSalesmanService.deleteMultipleEmBSalesmans(salesmanIdList);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    // 批量审批
    @RequestMapping("editMultipleEmBSalesmansStatus.do")
    @ResponseBody
    public Result editMultipleEmBSalesmansStatus(String salesmanIdList, HttpServletRequest req) {
        Result result = new Result();
        try {
            result = emBSalesmanService.editMultipleEmBSalesmansStatus(salesmanIdList);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "addSalesmanUser.do")
    @ResponseBody
    public Result addSalesmanUser(EmCUser emCUser, HttpServletRequest req) {
        Result result = new Result();
        try {
            result = emCUserService.insertUser(emCUser);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

}
